package com.example.hyg_veintype.utils;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;

import java.util.Stack;

/**
 * Created by yuyiwen on 2020/5/20.
 */

public class AppManager {
    public static Activity context = null;

    private static Stack<Activity> activityStack;
    private static AppManager instance;

    public AppManager() {
    }

    /**
     * 单一实例
     */
    public static AppManager getAppManager() {
        if (instance == null) {
            instance = new AppManager();
        }
        return instance;
    }

    /**
     * 添加Activity到堆栈
     */
    public void addActivity(Activity activity) {
        if (activityStack == null) {
            activityStack = new Stack<Activity>();
        }
        activityStack.add(activity);
    }

    /**
     * 获取当前Activity（堆栈中最后一个压入的）
     */
    public Activity currentActivity() {
        Activity activity = activityStack.lastElement();
        return activity;
    }

    /**
     * 结束当前Activity（堆栈中最后一个压入的）
     */
    public void finishActivity() {
        Activity activity = activityStack.lastElement();
        if (activity != null && !activity.isDestroyed()) {
            activityStack.remove(activity);
            activity.finish();
            activity = null;
        }
    }


    /**
     * 结束指定的Activity
     */
    public void finishActivity(Activity activity) {
        if (activity != null && !activity.isDestroyed() && !activity.isFinishing()) {
            activity.finish();
            activity = null;
        }
    }

    /**
     * 结束指定类名的Activity
     */
    public void finishActivity(Class<?> cls) {
        try {
            Stack<Activity> newActivityStack = new Stack<>();//新的activity集合
            Stack<Activity> needRemoveActivityStack = new Stack<>();//需要remove的activity
            for (Activity activity : activityStack) {
                if (activity.getClass().getName().equals(cls.getName())) {
                    needRemoveActivityStack.add(activity);
                } else {
                    newActivityStack.add(activity);
                }
            }
            activityStack.clear();
            activityStack.addAll(newActivityStack);
            for (int i = 0; i < needRemoveActivityStack.size(); i++) {
                finishActivity(needRemoveActivityStack.get(i));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * baseAcgtivity中的ondestroy调用
     */
    public void removeActivity(Class<?> cls) {
        try {
            Stack<Activity> newActivityStack = new Stack<>();//新的activity集合
            Stack<Activity> needRemoveActivityStack = new Stack<>();//需要remove的activity
            for (Activity activity : activityStack) {
                if (activity.getClass().getName().equals(cls.getName())) {
                    needRemoveActivityStack.add(activity);
                } else {
                    newActivityStack.add(activity);
                }
            }
            activityStack.clear();
            activityStack.addAll(newActivityStack);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 结束所有Activity
     */
    public void finishAllActivity() {
        try {
            if (activityStack != null && activityStack.size() > 0) {
                for (int i = 0, size = activityStack.size(); i < size; i++) {
                    if (null != activityStack.get(i)) {
                        activityStack.get(i).finish();
                    }
                }
                activityStack.clear();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 退出应用程序
     */
    public void AppExit(Context context) {
        try {
            finishAllActivity();
            ActivityManager activityMgr = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            activityMgr.restartPackage(context.getPackageName());
            System.exit(0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void clear() {
        if (null != activityStack) {
            activityStack.clear();
        }

    }

    /**
     * 堆棧是否為空
     *
     * @return bool
     */
    public boolean isNone() {
        return activityStack == null || activityStack.size() == 0;
    }
}
